package com.demon.framework.common.model.entity;

import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

/**
 * 分页信息
 */
@ApiModel(description = "分页信息")
public class Pager<T> implements Serializable {

    @ApiModelProperty(value = "当前页码")
    private int pageNum;

    @ApiModelProperty(value = "每页记录数")
    private int pageSize;

    @ApiModelProperty(value = "返回条数")
    private int size;

    @ApiModelProperty(value = "总页数")
    private int pages = 0;

    @ApiModelProperty(value = "是否有下一页")
    private boolean hasNextPage = false;

    @ApiModelProperty(value = "总数")
    private long total = 0;

    @ApiModelProperty(value = "结果集")
    private List<T> list;

    public static <T> Pager<T> of(List<T> list) {
        return new Pager<T>(list);
    }

    private Pager(List<T> list) {
        this.list = list;

        if (list instanceof Page) {
            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.pages = page.getPages();
            this.size = list.size();
            this.hasNextPage = this.size < this.pageSize;
            this.total = page.getTotal();
        } else if (list != null) {
            this.pageNum = 1;
            this.pageSize = list.size();
            this.pages = this.pageSize > 0 ? 1 : 0;
            this.size = list.size();
            this.hasNextPage = false;
            this.total = this.size;
        }
    }
}
